package com.zyx.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zyx.pojo.Headline;
import com.zyx.pojo.vo.PortalVo;
import com.zyx.service.HeadlineService;
import com.zyx.mapper.HeadlineMapper;
import com.zyx.util.JwtHelper;
import com.zyx.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 张杨煦
* @description 针对表【news_headline】的数据库操作Service实现
* @createDate 2024-09-08 14:40:17
*/
@Service
public class HeadlineServiceImpl extends ServiceImpl<HeadlineMapper, Headline>
    implements HeadlineService{

    @Autowired
    private HeadlineMapper headlineMapper;

    @Autowired
    private JwtHelper jwtHelper;

    @Override
    public Result updateData(Headline headline) {
        Integer version = headlineMapper.selectById(headline.getHid()).getVersion();
        //乐观锁
        headline.setVersion(version);
        headline.setUpdateTime(new Date());
        headlineMapper.updateById(headline);
        return Result.ok(null);
    }

    @Override
    public Result removeByHid(Integer hid) {
        headlineMapper.deleteById(hid);
        return Result.ok(null);
    }

    @Override
    public Result findNewsPage(PortalVo portalVo) {
        //1.进行分页数据查询  Mybatis-plus分页插件IPage
        IPage<Map> iPage = new Page<>(portalVo.getPageNum(),portalVo.getPageSize());
        headlineMapper.selectMyPage(iPage,portalVo);

        //2.分页数据拼接到result中
        Map data = new HashMap();
        data.put("pageData",iPage.getRecords());//当前页数据
        data.put("pageNum",iPage.getCurrent());//当前页码
        data.put("pageSize",iPage.getSize());//每页条数
        data.put("totalPage",iPage.getPages());//总页数
        data.put("totalSize",iPage.getTotal());//总条数

        Map pageInfo = new HashMap();
        pageInfo.put("pageInfo",data);

        return Result.ok(pageInfo);
    }

    @Override
    public Result showHeadlineDetail(Integer hid) {
        //1.查询获取版本号
        Map data = headlineMapper.queryDetailMap(hid);
        Map headLineMap = new HashMap<>();
        headLineMap.put("headline",data);
        //修改阅读量+1
        Headline headline = new Headline();
        headline.setHid((Integer) data.get("hid"));
        headline.setVersion(((Integer) data.get("version")));
        //阅读量+1
        headline.setPageViews(((Integer) data.get("pageViews"))+1);
        headlineMapper.updateById(headline);
        return Result.ok(headLineMap);
    }

    @Override
    public Result publish(String token, Headline headline) {
        //获取userId
        Integer userId = jwtHelper.getUserId(token).intValue();
        //通过userId相关信息进行装配
        headline.setPublisher(userId);
        headline.setCreateTime(new Date());
        headline.setUpdateTime(new Date());
        headline.setPageViews(0);
        //数据插入
        headlineMapper.insert(headline);
        return Result.ok(null);
    }

    @Override
    public Result findHeadlineByHid(Integer hid) {
        Headline headline = headlineMapper.selectById(hid);
        Map data = new HashMap<>();
        data.put("headline",headline);
        return Result.ok(data);
    }
}




